package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardFace;
import com.turing.post.cardmanagement.util.CardFaceDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Mar 11, 2013
 */
@Stateless(mappedName = "cardmanagement.impl.CardFaceDaoImpl")
@Remote
@Local
public class CardFaceDaoImpl implements CardFaceDao {

	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void delete(BigInteger cardFaceId) throws PCPSApplicationException {
		CardFace cf = getCardFaceByCardFaceId(cardFaceId);
		if (cf != null) {
			GeneralDao.remove(_em, cf);
		}
	}

	@Override
	public CardFace getCardFaceByCardFaceId(BigInteger cardFaceId) {
		return _em.find(CardFace.class, cardFaceId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardFace> getCardFaceDropDownList(PCPSWebInput input) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		String sql = "select * from CardFace where IssueCompanyNo='"
				+ issueCompanyNo + "' order by CardFaceId";
		Query query = _em.createNativeQuery(sql, CardFace.class);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardFace> getCardFaceListByCondition(PCPSWebInput input,
			String cardFaceName, Integer page) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return null;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		if (cardFaceName == null) {
			cardFaceName = "";
		}
		if (!PCPSUtil.isEmpty(cardFaceName)) {
			cardFaceName = " and cardFaceName like '%" + cardFaceName + "%'";
		}
		String sql = "select * from CardFace where IssueCompanyNo='"
				+ issueCompanyNo + "' " + cardFaceName + " order by CardFaceId";
		Query query = _em.createNativeQuery(sql, CardFace.class);
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getCardFaceTotalByCondition(PCPSWebInput input,
			String cardFaceName) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return BigInteger.ZERO;
		}
		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(input);
		if (cardFaceName == null) {
			cardFaceName = "";
		}
		if (!PCPSUtil.isEmpty(cardFaceName)) {
			cardFaceName = " and cardFaceName like '%" + cardFaceName + "%'";
		}
		String sql = "select nvl(count(*),0) from CardFace where IssueCompanyNo='"
				+ issueCompanyNo + "' " + cardFaceName + " order by CardFaceId";
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insert(CardFace cardFace) throws PCPSApplicationException {
		GeneralDao.insert(_em, cardFace);
	}

	@Override
	public void update(CardFace cardFace) throws PCPSApplicationException {
		GeneralDao.update(_em, cardFace);
	}

	/**
	 * 取某发卡商下的卡面图样列表
	 * 
	 * @param issueCompanyNo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<CardFace> getAllCardFaceByIssueCompanyNo(String issueCompanyNo) {
		String sql = "select * from cardFace c where c.issuecompanyno=?1 order by c.CardFaceId";
		Query query = _em.createNativeQuery(sql, CardFace.class);
		query.setParameter(1, issueCompanyNo);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public CardFace getCardFaceByCardSeqNo(PCPSWebInput input, String cardSeqNo) {
		if (input == null || PCPSUtil.isEmpty(input.getOperBranchNo())) {
			return null;
		}
		String sql = "select f.* from cardface f,cardstock t,makecardcontrol m"
				+ " where t.cardseqno = ?1 and t.batchno = m.batchno"
				+ " and m.cardfaceid = f.cardfaceid";
		Query query = _em.createNativeQuery(sql, CardFace.class);
		query.setParameter(1, cardSeqNo);
		List<CardFace> list = query.getResultList();
		if (null == list || list.size() <= 0) {
			return null;
		}
		return list.get(0);
	}
}
